package com.suwish.pc.util;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import com.android.ddmlib.FileListingService;
import com.suwish.device.util.Platform;

/**
 * 
 * @author Min
 *
 */
public final class ResourceUtils {

	static ClassLoader loder = ResourceUtils.class.getClassLoader();
	
	private static Map<String, ImageIcon> imageCache = new HashMap<String, ImageIcon>();
	
	public static final String UNKNOWN_FILE = "unknown";
	
	public static final String TYPE_FILE = "document.png";
	public static final String FILE_DIRECTORY = "";
	public static final String TYPE_DIRECTORY_LINK = "";
	public static final String TYPE_BLOCK = UNKNOWN_FILE;
	public static final String TYPE_CHARACTER = UNKNOWN_FILE;
	public static final String TYPE_LINK = UNKNOWN_FILE;
	public static final String TYPE_SOCKET = UNKNOWN_FILE;
	public static final String TYPE_FIFO = UNKNOWN_FILE;
	public static final String TYPE_OTHER = UNKNOWN_FILE;
	
	private ResourceUtils(){}
	
	
	public static URL load(String path){
		return loder.getResource(path);
	}
	public static URL loadResource(String path){
		return load("resources/" + path);
	}
	public static ImageIcon getIcon(String name){
		try{
//			String value = Platform.getAppResourcesDir() + File.separator + "images" + File.separator + name;
			ImageIcon icon = imageCache.get(name);
			if(icon != null)return icon;
//			System.out.println(value);
			URL url = loadResource("images/" +  name);
//			System.out.println(url);
			if(url == null) return null;
			imageCache.put(name, icon = new ImageIcon(url));
			return icon;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public static ImageIcon _Icon(String name){
		return getIcon(name);
	}
	public static String _I18N(String key){
		return null;
	}
	/**
	 * 文件图标命名协议。 </p>
	 * 
	 * 仅仅获得
	 * 
	 * @param key
	 * @return
	 */
	public static ImageIcon getFileIcon(String name){
		
		return _Icon("format-justify-center_22x22.png");
	}
	
	public static ImageIcon getFileIcon(int type){
		
		return _Icon(convertFileIconName(type));
	}
	public static String convertFileIconName(int fileType){
		switch(fileType){
		case FileListingService.TYPE_FILE :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_DIRECTORY :  return "Gnome-Folder-16x16.png";
		case FileListingService.TYPE_DIRECTORY_LINK :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_BLOCK :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_CHARACTER :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_LINK :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_SOCKET :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_FIFO :  return "format-justify-center_22x22.png";
		case FileListingService.TYPE_OTHER :  return "format-justify-center_22x22.png";
		
		default : return "format-justify-center_22x22.png";
		}
	}
}
